#include <iostream>
#include <cstdlib>
#include <random>
#include "Matrix.h"

/** 
 * To stop the program abnormally.
 * 
 * @param error_code code for error cases.
 */
void abort(int error_code)
{
    switch (error_code)
    {
    case 1:
	std::cout << "Need 3 arguments." << std::endl;
    getchar();
	break;
    default:
	break;
    }
    exit(-1);
};

int main(int argc, char *argv[])
{
    if (argc < 4)
	abort(1);
    int row = std::atoi(argv[1]);    //行数
    int col = std::atoi(argv[2]);    //列数


    double a[row*col];
    for(int i=0;i<row*col;i++)
    {
      a[i]= rand()/double(RAND_MAX);
    }
    
    Matrix<double> A(row,col,a);
    Matrix<double> B(col,row,a);
    Matrix<double> C =A*B;

    int right=0;
    int wrong=0;
    
    for(int i=0;i<row;i++)
    for(int j=0;j<row;j++)
    {
        double sum=0;
        for (int k=0;k<col;k++)
        {
            sum+=A.ele[i*col+k]*B.ele[k*row+j];
        }
        if (sum==C.ele[i*row+j])
        right++;
        else
        wrong++;
        
    }

    std::cout<<"matrix multiplication:"<<"\n";
    std::cout<<"Pass:"<<right<<"\t";                 ///到这里实现了矩阵乘法的验证
    std::cout<<"Fail:"<<wrong<<"\n";
    std::cout<<"Is_square"<<"  ";
    if(A.is_square()==(row==col))
    std::cout<<"Passed."<<"\n";
    else
    std::cout<<"Failed."<<"\n";                  ///实现方阵验证


    double b[row],c[row];
    for(int i=0;i<row;i++)
    {
      b[i]=rand()/double(RAND_MAX);
      c[i]=rand()/double(RAND_MAX);
    }
    RowVector<double> D(row,b);
    ColVector<double> E(row,c);

    double sum=0;
    double sum2=0;
    double sum3=0;
    for (int i=0;i<row;i++)
    {
        sum+=b[i]*c[i];
        sum2+=b[i]*b[i];
        sum3+=c[i]*c[i];
    }

    if(sum==D*E)
    std::cout<<"Vector multiplication passed."<<"\n";     ///实现向量乘法的验证
    else
    std::cout<<"Vector multiplication failed."<<"\n";

    if(sum2==D*D)
    std::cout<<"The inner product of row vectors passed."<<"\n";  ///实现行向量内积的验证
    else
    std::cout<<"The inner product of row vectors failed."<<"\n";



    if(sum3==E*E)
    std::cout<<"The inner product of col vectors passed."<<"\n";   ///实现列向量内积的验证
    else
    std::cout<<"The inner product of col vectors failed."<<"\n";
    
    
    Matrix<double> P=E*D;
    P.show();                                        ///验证列向量乘行向量的结果为矩阵
    

    Matrix<double> M(3,4);
    Matrix<double> N(3,4);
    RowVector<double> J(3);
    RowVector<double> K(2);
    ColVector<double> L(5);
    ColVector<double> T(6);
    if(atoi(argv[3])>0)
    std::cout<<" when error"<<atoi(argv[3])<<" happens(for example), the program will print:\n";
    



    switch (atoi(argv[3]))
    {
    case 0:
        break;                                /// no error
    case 1:
        J*L;                           ///错误1的验证
	break; 
    case 2:
        J*K;                           ///错误2的验证
	break;
    case 3:
        L*T;                            ///错误3的验证
	break;
    case 4:
        M*N;                            ///错误4的验证
	break;
    default:
	break;
    }


    return 0;


    

};
